package com.purchase.ml.mapper.order;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.purchase.ml.bean.req.order.OrderQueryReq;
import com.purchase.ml.bean.res.dashboard.DashboardConmonRes;
import com.purchase.ml.bean.res.dashboard.OrderRecentQueryRes;
import com.purchase.ml.bean.res.report.ReportMasterDearSaleRes;
import com.purchase.ml.bean.res.report.ReportOnlineTransactionRes;
import com.purchase.ml.bean.res.report.ReportProductSaleRes;
import com.purchase.ml.bean.res.report.vo.ReportOnlineVo;
import com.purchase.ml.bean.vo.dealer.DealerVo;
import com.purchase.ml.bean.vo.order.OrderCurCollectVO;
import com.purchase.ml.entity.acc.SysUser;
import com.purchase.ml.entity.order.SysDealerCollect;
import com.purchase.ml.entity.order.SysOrder;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * @description: 订单
 * @author kyyang
 * @date 2021/2/28 19:55
 * @version 1.0
 */
@Mapper
@Repository
public interface SysOrderMapper extends BaseMapper<SysOrder> {


    //暂时只处理一个月内数据
    OrderCurCollectVO selectCurDayOrderCollect(@Param("timeStr") String timeStr,@Param("dealerVos") List<Long> dealerVos,@Param("roleType") int roleType);

    /**
     * 通过订单号查询订单
     * @param oid
     * @return
     */
    @Select("select * from sys_order where oid=#{oid}")
    SysOrder findOrderByOid(@Param("oid") Long oid);

    @Update("UPDATE sys_order SET state = -1 where oid=#{oid}")
    int updateStateDel(@Param("oid") Long oid);

    /****
     * 做链接查询：住经销商或大代理最近出售的订单
    * */
    List<OrderRecentQueryRes> selectRecentOrderJoinDealer( @Param("dealerId") Long dealerId );

    /**
     * 查询经销商最近购买的订单
     * @param dealerId
     * @return
     */
    List<OrderRecentQueryRes> selectRecentBuyOrderDealer( @Param("dealerId") Long dealerId );
    /****
     * 管理员查询最近的订单；
     * */
    List<OrderRecentQueryRes> selectRecentOrderAdmin();

    /**
     * @description: 查询日期范围内总销售额
     * @author kyyang
     * @date 2021/4/11 13:51
     * @version 1.0
     */
    ReportOnlineVo selectOnlineTransactionReportToTalAmt(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("roleType") int roleType);

    /**
     * @description: 查询日期范围内销售明细
     * @author kyyang
     * @date 2021/4/11 13:51
     * @version 1.0
     */
    List<ReportOnlineTransactionRes> selectOnlineTransactionReportDetail(@Param("startTime") String startTime,@Param("endTime")String endTime,@Param("start") int start, @Param("end")int end,@Param("roleType") int roleType);

    /**
     * @description: 根据日期状态查询所有主经销商总销售
     * @author kyyang
     * @date 2021/4/11 14:55
     * @version 1.0
     */
    ReportOnlineVo selectMasterDealerSalesToTalAmt(@Param("startTime") String startTime,@Param("endTime")String endTime,@Param("orderState") int orderState,@Param("dealerName") String dealerName, @Param("roleType")int roleType,@Param("dealerId") Long dealerId);

    /**
     * @description: 根据日期状态查询所有主经销商明细
     * @author kyyang
     * @date 2021/4/11 14:55
     * @version 1.0
     */
    List<ReportMasterDearSaleRes> selectMasterDealerSalesDetail(@Param("startTime") String startTime,@Param("endTime")String endTime,@Param("orderState") int orderState,@Param("dealerName") String dealerName, @Param("roleType")int roleType,@Param("start") int start, @Param("end")int end,@Param("dealerId") Long dealerId);

    /**
     * @description: 查询日期范围内所有产品销售
     * @author kyyang
     * @date 2021/4/11 14:55
     * @version 1.0
     */
    ReportOnlineVo selectProductSalesReportToTalAmt(@Param("startTime") String startTime,@Param("endTime")String endTime,@Param("productName") String productName, @Param("roleType")int roleType);

    /**
     * @description: 查询日期范围内所有产品销售明细
     * @author kyyang
     * @date 2021/4/11 14:55
     * @version 1.0
     */
    List<ReportProductSaleRes> selectProductSalesReportDetail(@Param("startTime") String startTime,@Param("endTime")String endTime,@Param("productName") String productName, @Param("roleType")int roleType ,@Param("start") int start, @Param("end")int end);

//    @Select("select count(id) as totalPos,sum(case when state = 5 then 1 else 0 end) as totalCompletePos,sum(total_sales_cur) as totalSales from sys_dealer_collect where  = #{dealerId}")
    DashboardConmonRes sumPurchaseDealerOrder(@Param("dealerId") Long dealerId);

    /* 查询汇总数据：管理员住经销商经销商 汇总表全部完成的订单数*/
//    @Select("select sum(purchase_ord_num_cur) as totalPos,sum(purchase_ord_num_final_cur) as totalCompletePos,sum(total_sales_cur) as totalSales from sys_dealer_collect where parent_id = #{dealerId}")
    DashboardConmonRes sumPurchaseAgentOrMasterOrder(@Param("dealerId") Long dealerId);
}
